package star.基础.反向遍历链表;

import star.Utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class ReversePrint {
    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(3);
        head.next.next = new ListNode(2);
        Utils.traversal(reversePrint2(head));
    }

    public static int[] reversePrint(ListNode head) {
        if (head==null) return new int[0];
        ArrayList<Integer> list = new ArrayList<>();
        while (head.next!=null){
            list.add(head.val);
            head = head.next;
        }
        list.add(head.val);
        int size = list.size();
        int[] result = new int[size];
        for (int i = size-1; i >= 0; i--) {
            result[i] = list.get(size-i-1);
        }
        return result;
    }

    public static int[] reversePrint2(ListNode head) {
        if (head==null) return new int[0];
        int count = 1;
        ListNode cur = head;
        while (head.next!=null){
            count++;
            head = head.next;
        }
        int[] result = new int[count];
        for (int i = count-1; i >= 0; i--) {
            result[i] = cur.val;
            cur = cur.next;
        }
        return result;
    }
}
